
#include "Log.h"
#include "http.h"
#include "ArduinoJson.h"
#include  "NTPTime.h"
Log::Log(/* args */)
{
}

Log::~Log()
{
}

 
void sendLog(){
    if(!isPrefContainKey("applog")){
        return;
    }
    String log = getPreferenceString("applog");
    if(log.isEmpty()){
        return;
    }
    String url=WEB_SERVER_URL + "/log/addLog";


  JsonDocument doc;
  doc["deviceId"] = getEspId();
  doc["data"] = log;
  doc["time"] =  getRtcTime().getDateTime();
  char json[256];
  serializeJson(doc, json, sizeof(json));
   printStr("sendLog...");
   printStr("time",getRtcTime().getDateTime());
    requestPost(url,json,nullptr);
    removePreference("applog");
}

 

void putLog(LOG type,long time,String msg){
    if(isPrefContainKey("applog")){
        String log = getPreferenceString("applog");
        log.concat(",");
        log.concat(type);
        log.concat(":");
        log.concat(time);
        if(!msg.isEmpty()){
        log.concat(":");
        log.concat(msg);
        }

        putPreferenceString("applog",log);
    }else{
        String log ;
        log.concat(type);
        log.concat(":");
        log.concat(time);
        if(!msg.isEmpty()){
        log.concat(":");
        log.concat(msg);
        }
        putPreferenceString("applog",log);
    }
}
